package com.mald.plugin.http.client;

import com.mald.plugin.util.HttpClientUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author
 * @since 2018/4/4
 */
public class HttpClientResponse {
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientResponse.class);
    private CloseableHttpResponse response;

    private HttpClientUtil httpClientUtil;

    private CloseableHttpClient httpClient;

    public HttpClientResponse(
            HttpClientUtil httpClientUtil,
            CloseableHttpClient closeableHttpClient,
            CloseableHttpResponse closeableHttpResponse) {
        this.httpClientUtil = httpClientUtil;
        this.httpClient = closeableHttpClient;
        this.response = closeableHttpResponse;
    }

    /**
     * 关闭链接
     */
    public void close() throws IOException {
        try {
            httpClientUtil.releaseConnection(httpClient);
        } catch (IOException e) {
            LOGGER.error("释放链接异常！", e);
        }
        if (response != null) {
            response.close();
        }
    }

    /**
     * 获取HTTPResponse
     *
     * @return
     */
    public CloseableHttpResponse getResponse() {
        return this.response;
    }

}
